package com.pmonitor.util;


import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import weibo4j.Weibo;
import weibo4j.http.AccessToken;

public class SessionUtils {
	List<String> sessions = new ArrayList<String>();
	private static void setAttr(HttpServletRequest req, String key, Object value){
		HttpSession session = req.getSession();
		session.setMaxInactiveInterval(3600);
		session.setAttribute(key, value);
	}
	
	private static Object getAttr(HttpServletRequest req, String key){
		HttpSession session = req.getSession();
		return session.getAttribute(key);
	}
	
	public static void destoryAttr(HttpServletRequest req, String key){
		HttpSession session = req.getSession();
		session.removeAttribute(key);
	}
	
	public static void destoryAllAttrs(HttpServletRequest req){
		HttpSession session = req.getSession();
		session.invalidate();
	}
	
	public static List<String> getSessions(HttpServletRequest req){
		HttpSession session = req.getSession();
		List<String> sessionNames = new ArrayList<String>();
		Enumeration<String> enumeration = session.getAttributeNames();
		while (enumeration.hasMoreElements()) {
			String sessionName = (String) enumeration.nextElement();
			sessionNames.add(sessionName);
		}
		return sessionNames;
	}
	
	public static void setWeibo(HttpServletRequest req, Weibo value){
		setAttr(req, "weibo", value);
	}
	public static Weibo getWeibo(HttpServletRequest req){
		return (Weibo)getAttr(req, "weibo");
	}
	
	public static void setAccessToken(HttpServletRequest req, AccessToken accessToken){
		setAttr(req, "accesstoken", accessToken);
	}
	public static AccessToken getAccessToken(HttpServletRequest req){
		return (AccessToken)getAttr(req, "accesstoken");
	}
	
}